using System;
using System.Diagnostics;
namespace ServiceStack
{
	public static class PerfUtils
	{
		public static TimeSpan ToTimeSpan(this long fromTicks)
		{
			return TimeSpan.FromSeconds((double)fromTicks * 1.0 / (double)Stopwatch.Frequency);
		}
		public static double MeasureFor(Action fn, int runForMs)
		{
			int num = 0;
			Stopwatch stopwatch = new Stopwatch();
			stopwatch.Start();
			long num2 = 0L;
			while (num2 < (long)runForMs)
			{
				fn();
				num2 = stopwatch.ElapsedMilliseconds;
				num++;
			}
			return 1000.0 * (double)num2 / (double)num;
		}
		public static double Measure(Action fn, int times = 1, int runForMs = 2000, Action setup = null, Action warmup = null, Action teardown = null)
		{
			PerfUtils.<>c__DisplayClass2_0 <>c__DisplayClass2_ = new PerfUtils.<>c__DisplayClass2_0();
			<>c__DisplayClass2_.warmup = warmup;
			<>c__DisplayClass2_.fn = fn;
			<>c__DisplayClass2_.times = times;
			if (setup != null)
			{
				setup();
			}
			if (<>c__DisplayClass2_.warmup == null)
			{
				PerfUtils.<>c__DisplayClass2_0 expr_2E = <>c__DisplayClass2_;
				expr_2E.warmup = expr_2E.fn;
			}
			GC.Collect();
			PerfUtils.MeasureFor(delegate
			{
				<>c__DisplayClass2_.warmup();
			}, 100);
			double arg_6F_0 = PerfUtils.MeasureFor(delegate
			{
				for (int i = 0; i < <>c__DisplayClass2_.times; i++)
				{
					<>c__DisplayClass2_.fn();
				}
			}, runForMs);
			if (teardown != null)
			{
				teardown();
			}
			return arg_6F_0;
		}
	}
}
